﻿#pragma once
#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#ifndef __GD__GDUtil__
#define __GD__GDUtil__

#include "CrossApp.h"
#include "../C2DXShareSDK/C2DXShareSDK.h"
#include "../CommonHttpManager.h"

using namespace cn::sharesdk;


/* 辅助工具类 */
class GDUtil
{

public:
	virtual ~GDUtil();
	static void setViewHeight(CAView *view, int height); // 设置view高度为height
	static void setViewHeight(CAView *view, float height); // 设置view高度为height
	static void setViewHeight(CAView *view, CAView *footView,int space=0); // 设置view高度与footView底部一致
	static void setViewHeightFull(CAView *view); // 设置view高度正好被子节点填充满
	static int getViewHeight(CAView *view); // 获取节点高度
	static int getViewInnerHeight(CAView *view); // 获取节点内容高度（子节点高度）

	static void setViewWidth(CAView *view, int width); // 设置view宽度为width
	static void setViewWidth(CAView *view, float width); // 设置view宽度为width
	static void setViewWidth(CAView *view, CAView *subView); // 设置view宽度与subView右端一致

	static void setViewFollow(CAView *view1, CAView *view2); // 设置view2紧跟view1,竖向排列，依次向下排
	static void setViewsFollow(int count, ...); // 设置多个view依次跟随 count view的总数量，竖向排列，依次向下排
	static void setViewFollowHorizontal(CAView *view1, CAView *view2); // 设置view2紧跟view1，横向排列，依次向右排
	static void setViewsFollowHorizontal(int count, ...); // 设置多个view依次跟随 count view的总数量，竖向排列，依次向下排
	static void setViewFollowStep(int step,CAView *view1, CAView *view2); // 设置view2紧跟view1，保持step距离,竖向排列，依次向下排
	static void setViewsFollowStep(int step, int count, ...); // 设置多个view依次跟随 count view的总数量，保持step距离，竖向排列，依次向下排
	static void setViewFollowHorizontalStep(int step, CAView *view1, CAView *view2); // 设置view2紧跟view1，保持step距离，横向排列，依次向右排
	static void setViewsFollowHorizontalStep(int step, int count, ...); // 设置多个view依次跟随 count view的总数量，保持step距离，竖向排列，依次向下排

	
	static void subViewVerticalSort(CAView *view, int step=0); // 设置所有子节点竖向排列，依次向下排
	static void subViewHorizontalSort(CAView *view, int step=0); // 设置所有子节点横向排列，依次向右排

	static unsigned int getScaleHeight(CCSize size, int width); // 将size按指定宽度缩放获取缩放后高度
	static unsigned int getImageScaleHeight(CAImage *image, int width); // 将图片按指定宽度缩放获取缩放后高度

	static void setSubViewToCenter(CAView *superView, CAView *subView); // 设置 subView 在 superView的中心位置
	static void setSubViewToHorizontalCenter(CAView *superView, CAView *subView); // 设置 subView 在 superView 水平居中
	static void setSubViewToVerticalCenter(CAView *superView, CAView *subView); // 设置 subView 在 superView 垂直居中

	static CAView* getLastSubView(CAView *view); // 获取视图里面的最后一个子视图
	static int getMaxZOrderInSubView(CAView *view); // 获取所有子视图里面最大zorder

	/* 删除所有子元素除了制定的标签 */
	static void removeSubView(CAView *view, std::string exceptTextTag);
	/* 删除所有子元素，除了zorder层的元素 */
	static void removeSubViewExceptZorder(CAView *view, int zorder=1);

	/* 向当前NavigationController压入一个新的viewController */
	static void pushViewControllerToNavigationController(CANavigationController* navigationController, CAViewController *viewController, bool animated = true, bool needInit = true);
	static void pushViewController(CAViewController *viewController, bool animated = true, bool needInit = true);
	static void pushViewController(CANavigationController* navigationController, CAViewController *viewController);
	static void presentModalViewController(CAWindow *window, CAViewController *viewController, bool animated = true, const std::string& navigationBarBackGroundImage = "source_material/tabBar_selected_bg.png", bool isHiddenNavigationBar=false);
	/* 关闭当前视图 */
	static void closeViewController(CAViewController *viewController=NULL, bool animated=true);
	/* 关闭当前窗口直到root窗口 */
	static void closeToRootViewController(CAViewController *viewController = NULL, bool animated = true);
	/* 弹出三秒自动消失的提示task */
	static void task(const char* msg);
	/* 弹出alert提示 */
	static void alert(const char* msg);
	/* 延时弹出alert */
	static void alertDelay(const char* msg, float delay=0);
	static void alert(const char* msg, CAObject* target, SEL_CAAlertBtnEvent selector);
	static void confirm(const char* msg, CAObject* target, SEL_CAAlertBtnEvent selector, const char* title = "提示", const char* ok = "确定", const char* cancel = "取消");

	/* 字符串转数字 */
	static int stringToInt(std::string str);
	/* 数字转字符串 */
	static std::string intToString(int i);
	/* 字符串类型的数字 + 数字 */
	static std::string stringAdd(string str,int add);
	/* 打开分享面板 */
	static void showGDShareView(const std::string title, const std::string cnt, const std::string description, const std::string url = "", const std::string imageUrl = "", const std::string goodsId = "0", map<int, string> titleMap = map<int, string>(), map<int, string> cntMap = map<int, string>());
	
	/* 调用分享sdk方法 */
	static void share(cn::sharesdk::C2DXPlatType platType, std::string title, const std::string cnt, const std::string description, const std::string url = "", const std::string imageUrl = "", const std::string goodsId = "");

	static void share(std::string plantformId, std::string title, const std::string cnt, const std::string description, const std::string url = "", const std::string imageUrl = "", const std::string goodsId = "");
	/* 分享结果 */
	static void shareResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *shareInfo, CCDictionary *error);
	/* 第三方登录结果 */
	static void thirdOuthResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *error);
	/* 获取第三方资料 */
	static void getThirdUserResultHandler(C2DXResponseState state, C2DXPlatType platType, CCDictionary *userInfo, CCDictionary *error);
	
	/* 将字符串日期转化为结构体日期 */
	static void stringTimeToTm(struct tm& tm_,std::string stringTime, const std::string format = "%d-%d-%d");
	/* 将日期转字符串 */
	static std::string tmToStringTime(struct tm &tm, const char* format = "%d-%d-%d", std::string defaultStr = "");
	static std::string timetToStringTime(time_t &tt, const char* format = "%Y-%m-%d %H:%M:%S");
	static std::string timeToStringTime(struct tm &tm, const char* format = "%Y-%m-%d %H:%M:%S");
	static std::string timeToStringTime(long seconds, const char* format = "%Y-%m-%d %H:%M:%S");
	static std::string timeToStringTime(std::string seconds, const char* format = "%Y-%m-%d %H:%M:%S");

	/* 解析时间戳，如果小于当前时间不足1小时的显示分钟数，不足1天的显示小时数，不足3天的显示天数，其他的显示完整时间 */
	static std::string parseTime(long seconds, const char* format = "分享");
	/* 解析时间戳，如果小于当前时间不足1小时的显示分钟数，不足1天的显示小时数，不足3天的显示天数，其他的显示完整时间 */
	static std::string parseTime(std::string seconds, const char* format = "分享");


	/* 将秒转换为时期 */
	static std::string formatSecondsToDateTime(std::string seconds, const char* format = "%Y-%m-%d %H:%M:%S");
	
	
	// shader相关处理
	/* 设置圆形图片效果的shader */
	static void setShaderRircleImage(CAView *view);
	/* 设置高斯透明效果shader */
	static void setShaderBlurImage(CAView *view);

	// 
	static CALabel* createLabel(const char* title="", const CAColor4B& col = CAColor_black, int fontSize = _px(20), const char* fontName = "", const CATextAlignment& textAlignment = CATextAlignmentLeft, const CAVerticalTextAlignment& verticalTextAlignment=CAVerticalTextAlignmentTop);
	static CALabel* createLabelWithFrame(const CCRect& rect, const char* title = "", const CAColor4B& col = CAColor_black, int fontSize = _px(20), const char* fontName = "", const CATextAlignment& textAlignment = CATextAlignmentLeft, const CAVerticalTextAlignment& verticalTextAlignment = CAVerticalTextAlignmentTop);
	static CALabel* createLabelWithCenter(const CCRect& rect, const char* title = "", const CAColor4B& col = CAColor_black, int fontSize = _px(20), const char* fontName = "", const CATextAlignment& textAlignment = CATextAlignmentLeft, const CAVerticalTextAlignment& verticalTextAlignment = CAVerticalTextAlignmentTop);
	/* 设置label宽度刚好等于内容宽度 */
	static void setLabelFullWidth(CALabel *label,float maxWidth=0);
	static int getLabelWidth(CALabel *label);
	/* 获取lable高度 */
	static int getLabelHeight(CALabel *label);

	/* 保存图片 */
	static std::string saveImage(CAImage *image,std::string fileName);

	static CAColor4B parseColor(std::string color);

	// 动画
	static void runFavAction(CAView* view);
	// 解析url 参数
	static std::map<std::string, std::string> parseUrl(const std::string &url);

	/* 获取折扣信息 */
	static std::string getDiscount(std::string curPrice, std::string oldPrice);

	/* webview js回调处理 */
	static void jsCallBackFinished(const std::string &message,CAViewController *viewController=NULL);
    
    static unsigned long getFilePathSize(const std::string& path);
    
    static void getAllFilePaths(std::vector<std::string>& filePaths, std::vector<std::string>& dirPaths, const std::string& path);
    
    static void removeFilePath(const std::string& path);
	/* 删除文件 */
	static void removeFile(const std::string& file,bool removeImageCache=true);

	/* 将data数据保存到文件 */
	static void saveToFile(const std::string& fileName, const std::string& data, const std::string& filePath="data/");

    static void replace(std::string &s1, const std::string &s2, const std::string &s3);
	static int indexOf(const char *str, const char *dest);
	static char *substring(const char *str, int start, int len);
	static void toUpper(char* str);
	static bool isFullUrl(const std::string &url);
	static bool beginWith(const std::string str, const std::string needle);
	static bool endWith(const std::string str, const std::string needle);
	/* 剔除字符串里面的所有空格 \t isDeleteEnter=true会剔除\n \r */
	static void trimNull(std::string &s1,bool isDeleteEnter=true);
	/* 剔除字符串里面的回车换行和制表符全角空格 */
	static void trimTabEnter(std::string &s1);
	/* 获取字符串长度，一个汉字算1 */
	static int strLen(const std::string &in);
	/* 判断当前点击是否有效 */
	static bool isValidTouch();
    
    static void showMessage(){};
    
	/* 发送统计数据到服务器 */
	static void sendStatisticsToService(std::string type, const std::string& f1 = "", const std::string& f2 = "", const std::string& f3 = "", const std::string& f4 = "", const std::string& f5 = "", const std::string& f6 = "");
	/* 加密手机号中间四位 */
	static std::string encryptMobile(const std::string& mobile);
	/* 查找图片是否存在于缓存中 */
	static bool isImageInCache(const std::string& uri,const HttpGetImageType type = HttpGetImageDefault);
	/* 查找图片是否存在内存中 */
	static bool isImageInMemory(const std::string& uri);
	/* 计算坐标点与原点连线的角度[0,360] */
	static float getAngle(float x, float y);
	/* 计算 srcPoint 绕 centPoint 点旋转 rotation 度数（角度）后的新坐标  */
	static CCPoint getRotationAnglePoint(CCPoint centPoint, CCPoint srcPoint, float rotation);
protected:
private:
	static time_t lastTouchTime; // 最后一次点击时间
private:
	GDUtil();
};
#endif /* defined(__HelloCpp__ViewController__) */